package no.rmz.chez.game;

import java.util.Collection;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

import no.rmz.chez.chess.board.ChessBoard;
import no.rmz.chez.chess.board.Color;
import no.rmz.chez.chess.board.Coord;
import no.rmz.chez.chess.pieces.ChessPiece;

/**
 * Generate all possible moves on a chessboard.
 */
public final class MovementGenerator {

    /**
     * Hidden constructor, it should never be possible to
     * create an instance of an utility class.
     *
     */
    private MovementGenerator() {
    }

    /**
     * Get all possible moves that can be generated from a set of pieces.
     * for a particular board.
     * @param b      The board.
     * @param pieces The pieces that move around.
     */

    public static Collection<Move> getMoves(ChessBoard b, Map<Coord, ChessPiece> pieces) {
        final Collection<Move> result = new LinkedList<>();

        for (final Entry<Coord, ChessPiece> e: pieces.entrySet()) {
            final ChessPiece p = e.getValue();
            final Coord      c = e.getKey();
            p.addMoves(result, b, c);
        }
        return result;
    }

    /**
     * Find possible moves for pieces with a particular color.
     * @param b The board.
     * @param c The color
     * @return All the moves that can be performed by pieces of a particular color.
     */
    public static  Collection<Move> getPossibleMoves(
            final Color c,
            final ChessBoard b) {
        final Map<Coord, ChessPiece> myPieces = b.getPieces(c);
        final Collection<Move> myMoves = getMoves(b, myPieces);
        return myMoves;
    }
}
